package com.hqq.mybatisTest;


import com.hqq.mybatisTest.domain.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MyApp {
    public static void main(String[] args) throws IOException {

        //查询方法测试
        //访问mybatis读取student文件
        //1-定义mybatis主配置文件 从类路径target/clasees开始
        String config = "mybatis.xml";
        //2读取config表示的文件
        InputStream in = Resources.getResourceAsStream(config);
        //3创建sqlSessionFactoryBUilderd对象
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        //4创建sqlSessionFactory对象
        SqlSessionFactory factory = builder.build(in);
        //5创建sqlSession对象
        SqlSession sqlSession =factory.openSession();
        //6指定要执行的sql语句标识= sql映射文件中的namespace+"."+标签的id值
        String sql ="com.hqq.mybatisTest.dao.studentDao"+"."+"selectStudent";

        //7执行sql语句
        List<Student> result= sqlSession.selectList(sql);
        //8输出结果
       // System.out.println(result.get(0).getName());
        result.forEach(stu -> System.out.println(stu) );

        //9关闭session对象
        sqlSession.close();



    }
}
